import axios from 'axios';
import { userState } from '@/store/userStore';
import { responseCode } from '../responseCode';

const DEFAULT_CONFIG = {
  baseURL: '/api',
  timeout: 10000,
};


const api = axios.create(DEFAULT_CONFIG);
api.defaults.headers.post['Content-Type'] = 'application/json';

api.interceptors.request.use(
  config => {
    if (userState && userState.state.token)
      config.headers['Authentication'] = userState.state.token;
    return config;
  }
);

api.interceptors.response.use(
  response => {
    if (response.data.code === responseCode.TOKEN_INCORRECT) {
      location.href = "/";
    }
    return response;
  }
);

export { api };